/**
 * 2018年12月5日下午8:49:54
 * @author 谢进财
 */
package com.baihang.platform;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.DateKit;
import com.baihang.framework.kit.DbKit;
import com.baihang.framework.kit.HashKit;
import com.baihang.framework.kit.IdKit;

/**
 * 第一次使用时，基本信息创建，主要是管理员的信息创建
 *
 */
public class BaseInitTest {
	
	public static Map<String,String> addGroup() {
		Record record = new Record();
		String groupId = IdKit.getUUID();
		record.set("id", groupId);
		record.set("groupName", "总部");
		record.set("groupCode", "001");
		record.set("parentGroupId", "0");
		record.set("parentGroupCode", "0");
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql = DbKit.recordToSaveSql("system_group", record);
		Map<String,String> map = new HashMap<String,String>();
		map.put("groupId", groupId);
		map.put("sql", sql);
		return map;
	}
	public static Map<String,String> addUser(String groupId) {
		Record record = new Record();
		String userId = IdKit.getUUID();
		record.set("id", userId);
		record.set("userName", "admin");
		record.set("password", HashKit.md5("123456"));
		record.set("userCode", "001");
		record.set("chineseName", "管理员");
		record.set("groupCode", "001");
		record.set("groupId", groupId);
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql =DbKit.recordToSaveSql("system_user", record);
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId", userId);
		map.put("sql", sql);
		return map;
	}
	public static Map<String,String> addRole() {
		Record record = new Record();
		String roleId =IdKit.getUUID();
		record.set("id", roleId);
		record.set("roleName", "admin");
		record.set("roleCode", "001");		
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql=DbKit.recordToSaveSql("system_role", record);
		Map<String,String> map = new HashMap<String,String>();
		map.put("roleId", roleId);
		map.put("sql", sql);
		return map;
	}
	
	public static String addUserRole(String userId,String roleId) {
		Record record = new Record();
		record.set("id", IdKit.getUUID());
		record.set("userId", userId);
		record.set("userCode", "001");		
		record.set("roleId", roleId);		
		record.set("roleCode", "001");		
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql = DbKit.recordToSaveSql("system_userrole", record);
		return sql;
	}
	
	public static Map<String,String> addPermission() {
		Record record = new Record();
		String permissionId = IdKit.getUUID();
		record.set("id", permissionId);
		record.set("code", "001");
		record.set("parentCode", "0");		
		record.set("parentId", "0");
		
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql =DbKit.recordToSaveSql("system_permission", record);
		Map<String,String> map = new HashMap<String,String>();
		map.put("permissionId", permissionId);
		map.put("sql", sql);
		return map;
	}
	
	public static String addRoleAuthz(String roleId,String permissionId) {
		Record record = new Record();
		record.set("id", permissionId);
		record.set("roleId", roleId);
		record.set("roleCode", "001");		
		record.set("permissionId", permissionId);		
		record.set("permissionCode", "0");
		
		record.set("addUser", "财哥");
		record.set("addTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		record.set("modifyUser", "财哥");
		record.set("modifyTime", DateKit.getCurrentDate(DateKit.Format_DateTime));
		String sql = DbKit.recordToSaveSql("system_roleauthz", record);
		return sql;
	}
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-jdbc.xml");
		JdbcTemplate  jdbcTemplate = context.getBean("jdbcTemplate",JdbcTemplate.class);
		/*int result = jdbcTemplate.queryForObject("select count(*) from room ", int.class);
		System.err.println(result);*/
		
		//1
		Map<String,String> groupMap= BaseInitTest.addGroup();
		String groupId = groupMap.get("groupId");
		jdbcTemplate.update(groupMap.get("sql"));
		
		//2
		Map<String,String> userMap= BaseInitTest.addUser(groupId);
		String userId = userMap.get("userId");
		jdbcTemplate.update(userMap.get("sql"));
		
		//3
		Map<String,String> roleMap= BaseInitTest.addRole();
		String roleId = roleMap.get("roleId");
		jdbcTemplate.update(roleMap.get("sql"));
		
		//4
		jdbcTemplate.update(BaseInitTest.addUserRole(userId, roleId));	
		
		//5
		Map<String,String> permissionMap= BaseInitTest.addPermission();
		String permissionId = permissionMap.get("permissionId");
		jdbcTemplate.update(permissionMap.get("sql"));
		
		//6
		
		jdbcTemplate.update(BaseInitTest.addRoleAuthz(roleId, permissionId));
		//jdbcTemplate.update(BaseInitTest.addRoleAuthz("30ca2a3b8c96486c8ec87d5c121f5f38", permissionId));
	}

}
